# file: cces_figures.R
# Description: Creates figures from CCES Analysis in Fraga, Moskowitz and Schneer

#install.packages("foreign")

rm(list=ls())
library(foreign)

setwd("~/Desktop/replication")

#Load Data

bins.percev.12 <- read.dta("out/binned_perceived2012.dta")
bins.percev.14 <- read.dta("out/binned_perceived2014.dta")
ind <- read.dta("out/cces_panel_clean.dta")




#Figure 3
### 2012: perceivev district partisanship vs. district partisanship
pdf("out/scatter_perceived2012.pdf",width = 8, height=6)
par(mar=c(5.1,5.1,1.1,1.1))
par(las=1)
reg.line <- lm(ind$`_partisan_comp`[ind$year==2012] ~ ind$`_pvi`[ind$year==2012],
               weights=ind$weight[ind$year==2012])
plot(x=bins.percev.12$mean_pvi,y=bins.percev.12$mean_part_comp,
     pch=19,cex=bins.percev.12$sum_weights/150,col=adjustcolor("steelblue4",alpha.f=.8),
     #main="Turnout Rate by Competitiveness, 2010 & 2014",
     main="",
     ylab="", xlab="",
     xaxt="n",xlim=c(-30,30),ylim=c(-1,1))
# horizontal axis 
axis(1,at=seq(-30,30,10), labels=c("R+30","R+20","R+10","50/50","D+10","D+20","D+30"))
mtext("Perceived District Partisanship",side=2,line=3.8,cex=1,las=3)
mtext("District Partisanship",side=1,line=2.5,cex=1)
arrow.labels <- expression(paste("" %<-% "More Republican         Mixed         More Democratic" %->% ""))
mtext(arrow.labels,side=2,line=2.7,cex=.9,las=3)
lo <- loess(ind$`_partisan_comp`[ind$year==2012] ~ ind$`_pvi`[ind$year==2012], 
            family="gaussian",weights=ind$weight[ind$year==2012])
my.x <- ind$`_pvi`[ind$year==2012]
x1 <- seq(min(my.x), max(my.x), (max(my.x) - min(my.x))/1000)
lines(x1,predict(lo,x1), col='darkred', lwd=3, lty=2)
abline(reg.line,col="darkblue",lwd=3)
graphics.off()

#Figure A9
### 2014: perceivev district partisanship vs. district partisanship
pdf("out/scatter_perceived2014.pdf",width = 8, height=6)
par(mar=c(5.1,5.1,1.1,1.1))
par(las=1)
reg.line <- lm(ind$`_partisan_comp`[ind$year==2014] ~ ind$`_pvi`[ind$year==2014],
               weights=ind$weight[ind$year==2014])
plot(x=bins.percev.14$mean_pvi,y=bins.percev.14$mean_part_comp,
     pch=19,cex=bins.percev.14$sum_weights/150,col=adjustcolor("steelblue4",alpha.f=.8),
     #main="Turnout Rate by Competitiveness, 2010 & 2014",
     main="",
     ylab="", xlab="",
     xaxt="n",xlim=c(-30,30),ylim=c(-1,1))
# horizontal axis 
axis(1,at=seq(-30,30,10), labels=c("R+30","R+20","R+10","50/50","D+10","D+20","D+30"))
mtext("Perceived District Partisanship",side=2,line=3.8,cex=1,las=3)
mtext("District Partisanship",side=1,line=2.5,cex=1)
arrow.labels <- expression(paste("" %<-% "More Republican         Mixed         More Democratic" %->% ""))
mtext(arrow.labels,side=2,line=2.7,cex=.9,las=3)
lo <- loess(ind$`_partisan_comp`[ind$year==2014] ~ ind$`_pvi`[ind$year==2014], 
            family="gaussian",weights=ind$weight[ind$year==2014])
my.x <- ind$`_pvi`[ind$year==2014]
x1 <- seq(min(my.x), max(my.x), (max(my.x) - min(my.x))/1000)
lines(x1,predict(lo,x1), col='darkred', lwd=3, lty=2)
abline(reg.line,col="darkblue",lwd=3)
graphics.off()

#Figure 5
### 2012: any contact
bins.contact.12 <- read.dta("out/binned_contact2012.dta")
pdf("out/scatter_contact2012.pdf",width = 8, height=6)
par(mar=c(5.1,4.1,1.1,1.1))
par(las=1)
reg.line <- lm(ind$`_any_contact_`[ind$year==2012] ~ ind$aligned_pvi[ind$year==2012],
               weights=ind$weight[ind$year==2012])
plot(x=bins.contact.12$mean_pvi,y=bins.contact.12$mean_contact,
     pch=19,cex=bins.contact.12$sum_weights/150,col=adjustcolor("steelblue4",alpha.f=.8),
     #main="Turnout Rate by Competitiveness, 2010 & 2014",
     main="",
     ylab="", xlab="",
     xaxt="n",xlim=c(-30,30),ylim=c(0,1))
# horizontal axis 
axis(1,at=seq(-30,30,10), labels=c("-30","-20","-10","50/50","+10","+20","+30"))
mtext("Pr(Any Contact)",side=2,line=2.5,cex=1,las=3)
mtext("Partisan Alignment",side=1,line=3.7,cex=1)
arrow.labels <- expression(paste("" %<-% "Misaligned           Mixed               Aligned" %->% ""))
mtext(arrow.labels,side=1,line=2.5,cex=.9,las=1)
lo <- loess(ind$`_any_contact_`[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)] ~ ind$aligned_pvi[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)], 
            family="gaussian",weights=ind$weight[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)])
my.x <- ind$aligned_pvi[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)]
x1 <- seq(min(my.x), max(my.x), (max(my.x)-min(my.x))/1000)
lines(x1,predict(lo,x1), col='darkred', lwd=3, lty=2)
abline(reg.line,col="darkblue",lwd=3)
graphics.off()


#Figure 4
### 2012: can you make an ideological placement?
bins.ideo.12 <- read.dta("out/binned_ideo_eval2012.dta")
pdf("out/scatter_ideo_eval2012.pdf",width = 8, height=6)
par(mar=c(5.1,4.1,1.1,1.1))
par(las=1)
reg.line <- lm(ind$`_ideo_eval`[ind$year==2012] ~ ind$aligned_pvi[ind$year==2012],
               weights=ind$weight[ind$year==2012])
plot(x=bins.ideo.12$mean_pvi,y=bins.ideo.12$mean_eval,
     pch=19,cex=bins.ideo.12$sum_weights/150,col=adjustcolor("steelblue4",alpha.f=.8),
     #main="Turnout Rate by Competitiveness, 2010 & 2014",
     main="",
     ylab="", xlab="",
     xaxt="n",xlim=c(-30,30),ylim=c(0,1))
# horizontal axis 
axis(1,at=seq(-30,30,10), labels=c("-30","-20","-10","50/50","+10","+20","+30"))
mtext("Pr(Place Party's Candidate)",side=2,line=2.5,cex=1,las=3)
mtext("District Partisan Alignment",side=1,line=3.7,cex=1)
arrow.labels <- expression(paste("" %<-% "Misaligned           Mixed               Aligned" %->% ""))
mtext(arrow.labels,side=1,line=2.5,cex=.9,las=1)
lo <- loess(ind$`_ideo_eval`[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)] ~ ind$aligned_pvi[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)], 
            family="gaussian",weights=ind$weight[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)])
my.x <- ind$aligned_pvi[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)]
x1 <- seq(min(my.x), max(my.x), (max(my.x)-min(my.x))/1000)
lines(x1,predict(lo,x1), col='darkred', lwd=3, lty=2)
abline(reg.line,col="darkblue",lwd=3)
graphics.off()


#Figure A10
### any contact lowess for 2010, 2012, and 2014
### 2012: any contact
pdf("out/scatter_anycontact101214.pdf",width = 8, height=6)
par(mar=c(5.1,4.1,1.1,1.1))
par(las=1)
plot(0,
     pch="",
     #main="Turnout Rate by Competitiveness, 2010 & 2014",
     main="",
     ylab="", xlab="",
     xaxt="n",xlim=c(-30,30),ylim=c(.5,1))
# horizontal axis 
axis(1,at=seq(-30,30,10), labels=c("-30","-20","-10","50/50","+10","+20","+30"))
mtext("Pr(Any Contact)",side=2,line=2.5,cex=1,las=3)
mtext("Partisan Alignment",side=1,line=3.7,cex=1)
arrow.labels <- expression(paste("" %<-% "Misaligned           Mixed               Aligned" %->% ""))
mtext(arrow.labels,side=1,line=2.5,cex=.9,las=1)
lo.12 <- loess(ind$`_any_contact_`[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)] ~ ind$aligned_pvi[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)], 
            family="gaussian",weights=ind$weight[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)])
my.x <- ind$aligned_pvi[ind$year==2012 & !is.na(ind$Dpos1_Rneg1)]
x1 <- seq(min(my.x), max(my.x), (max(my.x)-min(my.x))/1000)
lines(x1,predict(lo.12,x1), col='darkred', lwd=3, lty=2)
lo.10 <- loess(ind$`_any_contact_`[ind$year==2010 & !is.na(ind$Dpos1_Rneg1)] ~ ind$aligned_pvi[ind$year==2010 & !is.na(ind$Dpos1_Rneg1)], 
            family="gaussian",weights=ind$weight[ind$year==2010 & !is.na(ind$Dpos1_Rneg1)])
lines(x1,predict(lo.10,x1), col='darkblue', lwd=3, lty=1)
lo.14 <- loess(ind$`_any_contact_`[ind$year==2014 & !is.na(ind$Dpos1_Rneg1)] ~ ind$aligned_pvi[ind$year==2014 & !is.na(ind$Dpos1_Rneg1)], 
               family="gaussian",weights=ind$weight[ind$year==2014 & !is.na(ind$Dpos1_Rneg1)])
lines(x1,predict(lo.14,x1), col='darkgreen', lwd=3, lty=3)
text(x=-28,y=.64,labels="2010",cex=.95,col = "darkblue", font=2)
text(x=27,y=.80,labels="2012",cex=.95,col = "darkred", font=2)
text(x=0,y=.66,labels="2014",cex=.95,col = "darkgreen", font=2)
graphics.off()
